// 接口域名

const oGetDataModule = {
    //   图片压缩模块
    upImgCompression(event){   // event：必传change的event
        // 压缩图片需要的一些元素和对象
        let imgArr = [];
          // 缩放图片需要的canvas
        let canvas = document.createElement('canvas');

        if(navigator.appName == "Microsoft Internet Explorer"&&parseInt(navigator.appVersion.split(";")[1].replace(/[ ]/g, "").replace("MSIE",""))<10){
            return new Promise(function  (resolve,reject) {
                alert('您的浏览器版本过低，请下载最新版本');
                return [];
            })
        }else{
            // 选择的文件对象
            let file = event.target.files;
            let context = canvas.getContext('2d');
            for(let i = 0;i<file.length;i++){

              var reader = new FileReader(),
                  imgFile = file[i];
              // 选择的文件是图片
              if(imgFile.type.indexOf("image") == 0) {
                  reader.readAsDataURL(imgFile);
              }else{
                  continue;
              }
              let img = new Image();
              // 文件base64化，以便获知图片原始尺寸
              reader.onload = function(e) {
                  img.src = e.target.result;
              };
              var imgP = new Promise(function  (resolve,reject) {
                      // base64地址图片加载完毕后
                      img.onload = function() {
                          // 图片原始尺寸
                          var originWidth = this.width;
                          var originHeight = this.height;
                          // 最大尺寸限制
                          var maxWidth = 400,
                              maxHeight = 400;
                          // 目标尺寸
                          var targetWidth = originWidth,
                              targetHeight = originHeight;
                          // 图片尺寸超过400x400的限制
                          if(originWidth > maxWidth || originHeight > maxHeight) {
                              if(originWidth / originHeight > maxWidth / maxHeight) {
                                  // 更宽，按照宽度限定尺寸
                                  targetWidth = maxWidth;
                                  targetHeight = Math.round(maxWidth * (originHeight / originWidth));
                              } else {
                                  targetHeight = maxHeight;
                                  targetWidth = Math.round(maxHeight * (originWidth / originHeight));
                              }
                          }
                          // canvas对图片进行缩放
                          canvas.width = targetWidth;
                          canvas.height = targetHeight;

                          // 清除画布
                          context.clearRect(0, 0, targetWidth, targetHeight);
                          // 图片压缩
                          context.drawImage(img, 0, 0, targetWidth, targetHeight);
                          var base64 = canvas.toDataURL("image/png",0.8);
                          
                          resolve(base64);
                      };
              });

              imgArr.push(imgP);
          }
            // canvas,context = null;
            return Promise.all(imgArr);
        }

    },


}


export default oGetDataModule;
